package com.connector.base.message.model;

import com.alibaba.fastjson.JSONObject;
import com.connector.base.connection.Connection;
import com.connector.base.protocol.Command;
import com.connector.base.protocol.Packet;
import com.google.common.base.Charsets;

public class PushMessage extends JsonBodyMessage
{
    private String type;
    public String data;

    public PushMessage() {
    }

    public PushMessage(final Packet packet, final Connection connection) {
        super(packet, connection);
    }

    @Override
    public void jsonToFields(final JSONObject jsonBody) {
        this.data = (jsonBody.containsKey((Object)"data") ? jsonBody.getString("data") : null);
        this.type = (jsonBody.containsKey((Object)"type") ? jsonBody.getString("type") : null);
    }

    @Override
    public JSONObject fieldToJson() {
        final JSONObject resultObj = new JSONObject();
        resultObj.put("data", (Object)this.data);
        resultObj.put("type", (Object)this.type);
        return resultObj;
    }

    public static Packet buildPacket(final String msg) {
        final Packet packet = new Packet();
        packet.setCmd(Command.PUSH.cmd);
        packet.setBodyLength(msg.length());
        packet.setType((byte)1);
        packet.setBody(msg.getBytes(Charsets.UTF_8));
        return packet;
    }

    public String getType() {
        return this.type;
    }

    public String getData() {
        return this.data;
    }

    public void setType(final String type) {
        this.type = type;
    }

    public void setData(final String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "PushMessage(type=" + this.getType() + ", data=" + this.getData() + ")";
    }
}
